#include "data.h"
#include <iostream>
#include <thread>

/* Mutex de protection de l'écran */
static mutex m;

static void thread1(Data& d) {
	lock_guard<mutex> g(m);
	cout << "Thread 1 ajoute 3" << endl;
	d.ajouter(3);
	d.afficher();
	cout << endl;
}

static void thread2(Data& d) {
	lock_guard<mutex> g(m);
	cout << "Thread 2 ajoute la liste [1, 4, 5, 2, 7]" << endl;
	list<int> liste;
	liste.push_back(1);
	liste.push_back(4);
	liste.push_back(5);
	liste.push_back(2);
	liste.push_back(7);
	d.ajouter(liste);
	d.afficher();
	cout << endl;
}

static void thread3(Data& d) {
	lock_guard<mutex> g(m);
	cout << "Thread 3 cherche 2" << endl;
	if (d.trouve(2)) {
		cout << "2 est trouve" << endl;
	} else {
		cout << "2 n'est pas trouve" << endl;
	}
	d.afficher();
	cout << endl;
}

static void thread4(Data& d) {
	lock_guard<mutex> g(m);
	cout << "Thread 4 lit la liste" << endl;
	d.afficher();
	cout << endl;
}

int main(int argc, char** argv) {
	Data d;
	thread t1(thread1, ref(d));
	thread t2(thread2, ref(d));
	thread t3(thread3, ref(d));
	thread t4(thread4, ref(d));
	t2.join();
	t4.join();
	t3.join();
	t1.join();
	return 0;
}

